package com.multi.thread.chapter07.demo03;

public class Run {
	
	//enumerate将当前线程的线程组及其子组中的每个活动线程复制到指定的数组中
	public static void main(String[] args) {
		ThreadGroup mainGroup = Thread.currentThread().getThreadGroup();
		ThreadGroup a = new ThreadGroup(mainGroup, "A");
		Runnable run = new Runnable() {
			@Override
			public void run() {
				try {
					System.out.println("runMethod!");
					Thread.sleep(10000); //线程必须在运行状态才可以受组管理
				} catch (InterruptedException e) {
					e.printStackTrace();
				} 
			}
		};
		
		Thread thread = new Thread(a, run);
		thread.setName("Z");
		thread.start();
		
		ThreadGroup[] listGroup = new ThreadGroup[Thread.currentThread().getThreadGroup().activeGroupCount()];
		Thread.currentThread().getThreadGroup().enumerate(listGroup);
		System.out.println("main线程中有多少个子线程组：" + listGroup.length + ", 名称为：" + listGroup[0].getName());
		
		Thread[] listThread = new Thread[listGroup[0].activeCount()];
		listGroup[0].enumerate(listThread);
		System.out.println(listThread[0].getName());
	}

}
